import {Component, Input, OnInit} from "@angular/core";
import {Favorite, FavoriteGroup, UserGroup} from "@shared/models/UserInfo";
import {InputDialogContent} from "@/app/pages/common/input-dialog-content";
import {FavoriteActionReq, UserActionService} from "@core/service/UserActionService";
import {NzModalService} from "ng-zorro-antd/modal";
import {BBYUtils} from "@shared/utils/BBYUtils";
import {NzMessageService} from "ng-zorro-antd/message";
import {AuthService} from "@core/service/AuthService";

@Component({
  selector: 'bby-add-favorite-dialog',
  template:
    `
      <ng-container >
        <div style="margin-bottom: 10px">
          收藏名：
          <input nz-input style="width:300px;" [ngModel]="favoriteName"/>
        </div>
        <div>
          收藏夹：
          <nz-select [(ngModel)]="groupId" style="width: 300px;" >
            <nz-option *ngFor="let group of groups"
                       [nzLabel]="group.name" [nzValue]="group.id">
            </nz-option>
          </nz-select>
          <a style="margin-left: 5px" (click)="addGroupClick()"><nz-tag><i nz-icon nzType="plus"></i>新建</nz-tag></a>
        </div>

      </ng-container>

    `
})
export class AddFavoriteDialog implements OnInit{
  @Input()
  groups:FavoriteGroup[]=[]
  favoriteName:string=''
  @Input()
  userId:string=''
  groupId='';

  constructor(
    private dialogService: NzModalService,
    private userActionService: UserActionService,
    private message: NzMessageService
  ) {
  }
  ngOnInit(): void {
    this.groupId=this.groups[0].id
  }

  addGroupClick() {
    let loading = false;
    this.dialogService.create({
      nzTitle: '新建收藏夹', nzWidth: '400px',
      nzContent: InputDialogContent,
      nzComponentParams: {
        placeholder:'请输入收藏夹名'
      },
      nzOnOk: (page) =>
        new Promise((resolve,reject) => {
          loading = true
          let data:FavoriteActionReq = {
            opera:'addGroup',
            newName:page.value
          }
          BBYUtils.handelResult(
            this.userActionService.setFavorites(data),
            res => {
              this.groups=res.data
              AuthService.saveUserInfo(res.data)
              resolve()
            },
            res => {
              this.message.error(res.msg)
              reject()
            }
          )
        })
    })
  }
}
